# -*- coding: utf-8 -*-
# @Time     : 2021/11/27 17:00
# @Author   : wljess
# @File     : 上下文管理器管理锁.py
# @Describe : 
# @Software : PyCharm

import time
import threading


class Singleton:
    """单例模式————最多只允许创建一个该类实例"""
    _lock = threading.Lock()
    _instance = None

    def __new__(cls, *args, **kwargs):
        time.sleep(1)   # 模拟高并发情况
        with cls._lock:
            if not cls._instance:
                cls._instance = object.__new__(cls)

        return cls._instance

def task(a, b):
    s = Singleton(a, b)
    print(s)


for i in range(5):
    t = threading.Thread(target=task, args=(2, 3))
    t.start()



